<?php

/*
 * [HZTCcms] (C)2001-2018 AoSheWeb Inc.
 * This is NOT a freeware, use is subject to license terms.
 * 
 * Document   : Node.class.php
 * Encoding   : UTF-8
 * Created on : 2011-11-25, 12:05:01
 * Author     : leefire yunzhi.li.08@gmail.com 464328282
 * Site       : http://www.aosheweb.com
 * Description: 
 */

class NodeAction extends CommonAction {
    /*
     * 节点管理首页模板显示控制方法
     * @access	public
     * @param	void
     * @return	void
     */

    public function index() {
	// 应用或是分组列表
	$list = $this->getChildrenNode(0);
	// 对应的模型列表
	foreach ($list as $key => $val) {
	    $list[$key]['module'] = $this->getChildrenNode($val['id']);
	}
	$this->assign('list', $list);
	$this->display();
    }

    /*
     * 
     */

    public function nodeList() {
	$map = array();
	// 路径条件
	if ($_REQUEST['path'] != '') {
	    $path = $_REQUEST['path'];
	    $map['nodepath'] = array('like', $path . '%');
	}
	// 状态条件
	if($_REQUEST['status'] != ''){
	    $status = $_REQUEST['status'];
	    $map['status'] = array('eq', $status);
	}
	// 标题条件
	if($_REQUEST['title'] != ''){
	    $title = $_REQUEST['title'];
	    $map['nodetitle'] = array('like', '%' . $title . '%');
	}
	// 按条件检索节点
	$this->getNodeList($map);
	$this->display();
    }

    /*
     * 
     */

    public function getNodeList($map, $order = '') {
	$node = M('Node');
	$count = $node->where($map)->count('id');
	// 如果存在有节点数据
	if ($count > 0) {
	    import('@.ORG.Page');
	    //创建分页对象
	    if (!empty($_REQUEST [C('VAR_PERPAGE')])) {
		$listRows = $_REQUEST [C('VAR_PERPAGE')];
	    } else {
		$listRows = '';
	    }

	    $p = new Page($count, $listRows);
	    //分页查询数据
	    $nodelist = $node->where($map)->order('nodesort desc')->limit($p->firstRow . ',' . $p->listRows)->findAll();

	    //分页跳转的时候保证查询条件
	    foreach ($map as $key => $val) {
		if (!is_array($val)) {
		    $p->parameter .= "$key=" . urlencode($val) . "&";
		}
	    }
	    //分页显示
	    $page = $p->show();
	    //模板赋值显示
	    $this->assign('nodelist', $nodelist);
	    $this->assign("page", $page);
	}
	$this->assign('totalCount', $count);
	$this->assign('numPerPage', C('PAGE_LISTROWS'));
	$this->assign('currentPage', !empty($_REQUEST[C('VAR_PAGE')]) ? $_REQUEST[C('VAR_PAGE')] : 1);
    }

    /*
     * 获取直接子节点
     * @access	public
     * @param	string		$nid		当前节点的ID
     * @return	array		$list		返回孩子节点数组
     */

    public function getChildrenNode($nid) {
	$node = M('Node');
	$list = $node->where(array('nodepid' => $nid))->findAll();
	return $list;
    }

    /*
     * 
     */

    public function actionList() {
	$path = $_GET['path'];
	if (!empty($_POST['path'])) {
	    $path = $_POST['path'];
	}
	$status = $_POST['status'];
	$title = $_POST['title'];

	$map['nodelevel'] = array('eq', 3);
	$map['nodepath'] = array('like', $path . '%');
	$map['nodetitle'] = array('like', '%' . $title . '%');

	if ($status != "") {
	    $map['status'] = array('eq', $status);
	}
	dump($map);
	$list = $this->getActionList($map);
	$this->assign('actionList', $list);
	$this->display();
    }

    /*
     * 
     */

    public function getActionList($map) {
	$node = M('Node');

//	$voList = array();
//	$count = $node->where($map)->count('id');
//	if ($count > 0) {
//	    import('@.ORG.Page');
//	    //创建分页对象
//	    if (!empty($_REQUEST [C('VAR_PERPAGE')])) {
//		$listRows = $_REQUEST [C('VAR_PERPAGE')];
//	    } else {
//		$listRows = '';
//	    }
//
//	    $p = new Page($count, $listRows);
//	    //分页查询数据
//	    $voList = $node->where($map)->order('nodesort desc')->limit($p->firstRow . ',' . $p->listRows)->findAll();
//
//	    //分页跳转的时候保证查询条件
//	    foreach ($map as $key => $val) {
//		if (!is_array($val)) {
//		    $p->parameter .= "$key=" . urlencode($val) . "&";
//		}
//	    }
//	    //分页显示
//	    $page = $p->show();
//	    //模板赋值显示
//	    $this->assign('actionList', $voList);
//	    $this->assign("page", $page);
//	}
//	$this->assign('totalCount', $count);
//	$this->assign('numPerPage', !empty($_REQUEST [C('VAR_PERPAGE')]) ? $_REQUEST [C('VAR_PERPAGE')] : 20);
//	$this->assign('currentPage', !empty($_REQUEST[C('VAR_PAGE')]) ? $_REQUEST[C('VAR_PAGE')] : 1);

	$list = $node->where($map)->order('nodesort desc')->findAll();
	return $list;
    }

    /*
     * 获取操作的节点的信息
     * @access  public
     * @param   string		$fields     所需要信息的字段
     * @param   string		$app        对应的应用或是分组的名称
     * @param   string		$module     对应的模块名称
     * @param   string		$action     对应的操作名称
     * @return  array		$nodeinfo   返回查询的结果
     */

    public function getNodeInfo($fields='id', $app=APP_NAME, $module=MODULE_NAME, $action=ACTION_NAME) {
	$node = M('Node');
	$appid = $node->where(array('nodename' => $app, 'nodelevel' => 1))->getField('id');
	$moduleid = $node->where(array('nodename' => $module, 'nodelevel' => 2, 'nodepid' => $appid))->getField('id');
	$nodeinfo = $node->where(array('nodename' => $action, 'nodelevel' => 3, 'nodepid' => $moduleid))->getField($fields);
	return $nodeinfo;
    }

}

?>
